package com.java.util;

import com.java.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


public class MyInterHandler implements HandlerInterceptor {

    private List<String> excludeUrls;

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
       //获取对应的Session数据
        User users=(User)httpServletRequest.getSession().getAttribute("userInfo");

        //获取请求的路径地址
        String requestURI = httpServletRequest.getRequestURI();

        for (String excludeUrl : excludeUrls) {
            if (requestURI.contains(excludeUrl)) {
                return true;
            }
        }
        if (users==null) {
            //跳转的页面
            /*httpServletResponse.sendRedirect("/view/login.jsp");*/
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }

    public List<String> getExcludeUrls() {
        return excludeUrls;
    }

    public void setExcludeUrls(List<String> excludeUrls) {
        this.excludeUrls = excludeUrls;
    }
}
